package com.ef.service.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.ef.constant.StatusType;
import com.ef.dao.TeacherDao;
import com.ef.domain.Teacher;
import com.ef.service.TeacherService;
import com.ef.util.CalendarUtil;
import com.ef.util.CustomExample;
import com.ef.util.PaginationSupport;

public class TeacherServiceImpl extends BaseServiceImpl implements
		TeacherService {

	private TeacherDao teacherDao;

	public void delete(Teacher entity) {
		entity.setStatus(StatusType.INVALID);
		teacherDao.update(entity);

	}

	public PaginationSupport<Teacher> findByExample(
			CustomExample<Teacher> example, int startIndex, int pageSize) {

		return teacherDao.findPageByExample(example, startIndex, pageSize);
	}

	public Teacher findById(Long id) {

		return teacherDao.findById(id);
	}

	public void saveOrUpdate(Teacher entity) {
		teacherDao.clear();
		teacherDao.saveOrUpdate(entity);
	}

	public TeacherDao getTeacherDao() {
		return teacherDao;
	}

	public void setTeacherDao(TeacherDao teacherDao) {
		this.teacherDao = teacherDao;
	}

	public List<Teacher> getAllValidTeachers() {
		DetachedCriteria criterion = DetachedCriteria.forClass(Teacher.class);
		criterion.add(Restrictions.eq("status", StatusType.VALID));
		criterion.add(Restrictions.eq("client", false));
		criterion.add(Restrictions.le("startDate",CalendarUtil.getBeginOfDay(new Date())));
		criterion.add(Restrictions.ge("endDate",CalendarUtil.getEndOfDay(new Date())));
		criterion.addOrder(Order.asc("address"));
		return teacherDao.findAllByCriteria(criterion);
	}

	
	public List<Teacher> getAllValidCLTTeachers() {
		DetachedCriteria criterion = DetachedCriteria.forClass(Teacher.class);
		criterion.add(Restrictions.eq("status", StatusType.VALID));
		criterion.add(Restrictions.eq("client", true));
		criterion.add(Restrictions.le("startDate",CalendarUtil.getBeginOfDay(new Date())));
		criterion.add(Restrictions.ge("endDate",CalendarUtil.getEndOfDay(new Date())));
		criterion.addOrder(Order.asc("address"));
		return teacherDao.findAllByCriteria(criterion);
	}
	
	public List<Teacher> findByName(String name, int max) {
		StringBuffer hql =  new StringBuffer("From Teacher where lower(name) like '%");
		hql.append(name);
		hql.append("%' and status='VALID'");
	    return teacherDao.findMultiAll(hql.toString(), 0, max);
	}

	public List<Teacher> findByNameInSchool(String name,Long schoolId,int max) {
		
//		DetachedCriteria criterion=DetachedCriteria.forClass(Teacher.class);
//		criterion.add(Restrictions.like("lower(name)",name.toLowerCase(),MatchMode.ANYWHERE));
//	    criterion.createCriteria("school").add(Restrictions.eq("id",schoolId));
//		criterion.add(Restrictions.eq("status",
//				StatusType.VALID));
//		System.out.println(criterion.toString());
		StringBuffer hql =  new StringBuffer("From Teacher where lower(name) like '%");
		hql.append(name);
		hql.append("%'  and status='VALID' and school.id=");
		hql.append(schoolId);
		
	    return teacherDao.findMultiAll(hql.toString(), 0, max);
 	}
}
